package com.css.bjdt_core.reportExplainFill.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.dnDzz.entity.DnDzz;
import com.css.bjdt_core.explain.entity.RptCellExplain;
import com.css.bjdt_core.explain.entity.RptTemplateExplain;
import com.css.bjdt_core.explain.service.ReportTemplateExplainService;
import com.css.bjdt_core.explain.service.RptCellExplainService;
import com.css.bjdt_core.reportExplainFill.entity.ReportExplainFillEntity;
import com.css.bjdt_core.reportExplainFill.entity.ReportExplainTextFillEntity;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.ReportConstant;

@Service
public class RptCellExplainSummaryService {
	@Autowired
	private Dao dao;

	@Autowired
	private RptExplainCellFillService rptExplainCellFillService;

	@Autowired
	private RptCellExplainService rptCellExplainService;

	@Autowired
	private ReportTemplateExplainService reportTemplateExplainService;

	/**
	 * 汇总报表说明批注。条目型批注
	 * @param dzzIdList 需要汇总的党组织集合
	 * @param explainIdList 需要汇总的报表说明
	 * @param annualId 年度ID
	 * @param summaryDzzId 汇到哪个党组织下
	 */
	public void rptCellExplainSummaryTiaoMu(List<String> dzzIdList, List<String> explainIdList, String annualId,
			String summaryDzzId) {
		List<ReportExplainFillEntity> reportExplainFillEntityList = new ArrayList<ReportExplainFillEntity>();
		for (String explainId : explainIdList) {
			Integer num = 0;
			for (String dzzId : dzzIdList) {
				List<ReportExplainFillEntity> list = rptExplainCellFillService
						.getAllExamineInfoByTemplateDataId(explainId, annualId, dzzId);
				Map<String, String> mapRow = getRowMap(annualId, dzzId, explainId, num);
				for (ReportExplainFillEntity reportExplainFillEntity : list) {
					reportExplainFillEntity.setExplainCellDataId(CommonUtil.getUUID());
					reportExplainFillEntity.setSumStatus("1");
					reportExplainFillEntity.setDzzId(summaryDzzId);
					reportExplainFillEntity.setAbsRowNum(mapRow.get(reportExplainFillEntity.getAbsRowNum()));
					reportExplainFillEntity.setIsSameRow(dzzId);
					reportExplainFillEntityList.add(reportExplainFillEntity);
				}
				num = num + getCountByDzzIdAndExplainId(annualId, dzzId, explainId);
			}
		}
		rptExplainCellFillService.insertExplainCellValueList(annualId, reportExplainFillEntityList);
	}

	/**
	 * 汇总报表说明批注。数字型批注
	 * @param dzzIdList 需要汇总的党组织集合
	 * @param explainIdList 需要汇总的报表说明
	 * @param annualId 年度ID
	 * @param summaryDzzId 汇到哪个党组织下
	 */

	public void rptCellExplainSummaryShuZi(List<String> dzzIdList, List<String> explainIdList, String annualId,
			String summaryDzzId) {
		List<ReportExplainFillEntity> reportExplainFillEntityList = new ArrayList<ReportExplainFillEntity>();

		String strDzzId = "";
		for (String dzzId : dzzIdList) {
			strDzzId = strDzzId + "'" + dzzId + "'" + ",";
		}
		strDzzId = strDzzId.substring(0, strDzzId.length() - 1);

		String tableName = ReportConstant.EXPLAIN_TABLE_PREFIX + annualId;
		//String strSql = ReportConstant.EXPLAIN_QUERY_BYCOLANDROW_SQL.replace("$table_name", tableName);
		String strSql = "select sum(EXPLAIN_CELL_VALUE) from " + tableName + " where "
				+ "EXPLAIN_ID = @EXPLAIN_ID and DZZ_ID in (" + strDzzId + ") and ABS_ROW_NUM = @ABS_ROW_NUM "
				+ "and ABS_COL_NUM = @ABS_COL_NUM and SUM_STATUS is null";
		Sql sql = Sqls.create(strSql);
		sql.setCallback(Sqls.callback.integer());

		for (String explainId : explainIdList) {
			List<RptCellExplain> rptCellExplainList = rptCellExplainService.queryAllCellExplainByExplainId(explainId);
			for (RptCellExplain rptCellExplain : rptCellExplainList) {
				sql.params().set("EXPLAIN_ID", explainId).set("ABS_ROW_NUM", rptCellExplain.getAbsRowNum())
						.set("ABS_COL_NUM", rptCellExplain.getAbsColNum());
				dao.execute(sql);
				int explainCellValue = sql.getObject(Integer.class);
				ReportExplainFillEntity reportExplainFillEntity = new ReportExplainFillEntity();
				reportExplainFillEntity.setExplainId(CommonUtil.getUUID());
				reportExplainFillEntity.setAbsColNum(rptCellExplain.getAbsColNum() + "");
				reportExplainFillEntity.setAbsRowNum(rptCellExplain.getAbsRowNum() + "");
				reportExplainFillEntity.setExplainCellValue(explainCellValue + "");
				reportExplainFillEntity.setDzzId(summaryDzzId);
				reportExplainFillEntity.setSumStatus("1");
				reportExplainFillEntity.setExplainId(explainId);
				reportExplainFillEntity.setIsSameRow(summaryDzzId);
				reportExplainFillEntityList.add(reportExplainFillEntity);
			}
		}

		rptExplainCellFillService.insertExplainCellValueList(annualId, reportExplainFillEntityList);
	}

	/**
	 * 汇总报表说明批注。文本型批注
	 * @param dzzIdList 需要汇总的党组织集合
	 * @param explainIdList 需要汇总的报表说明
	 * @param annualId 年度ID
	 * @param summaryDzzId 汇到哪个党组织下
	 */

	public void rptCellExplainSummaryWenBen(List<String> dzzIdList, List<String> explainIdList, String annualId,
			String summaryDzzId) {
		List<ReportExplainFillEntity> reportExplainFillEntityList = new ArrayList<ReportExplainFillEntity>();
		List<ReportExplainTextFillEntity> reportExplainTextFillEntityList = new ArrayList<ReportExplainTextFillEntity>();

		String tableName = ReportConstant.EXPLAIN_TABLE_PREFIX + annualId;
		String strSql = ReportConstant.EXPLAIN_QUERY_BYCOLANDROW_SQL.replace("$table_name", tableName);
		Sql sql = Sqls.create(strSql);
		sql.setCallback(Sqls.callback.str());

		for (String explainId : explainIdList) {
			List<RptCellExplain> rptCellExplainList = rptCellExplainService.queryAllCellExplainByExplainId(explainId);
			for (RptCellExplain rptCellExplain : rptCellExplainList) {
				String str = "";
				for (String dzzId : dzzIdList) {
					sql.params().set("EXPLAIN_ID", explainId).set("DZZ_ID", dzzId)
							.set("ABS_ROW_NUM", rptCellExplain.getAbsRowNum())
							.set("ABS_COL_NUM", rptCellExplain.getAbsColNum());
					dao.execute(sql);
					String explainCellValue = sql.getObject(String.class);
					if (explainCellValue == null) {
						continue;
					}
					String tableNameText = ReportConstant.EXPLAIN_TEXT_TABLE_PREFIX + annualId;
					String strSqlText = ReportConstant.EXPLAIN_TEXT_QUERY_SQL.replace("$table_name", tableNameText);
					Sql sqlText = Sqls.create(strSqlText);
					sqlText.setCallback(Sqls.callback.str());
					sqlText.params().set("explainTextId", explainCellValue);
					dao.execute(sqlText);
					String explainCellText = sqlText.getObject(String.class);
					str = (str.equals("")) ? (explainCellText) : (str + "," + explainCellText);
				}
				//先处理子表，就是报表说明的文本型批注的表
				ReportExplainTextFillEntity reportExplainTextFillEntity = new ReportExplainTextFillEntity();
				String explainCellValueId = CommonUtil.getUUID();
				reportExplainTextFillEntity.setExplainCellValueId(explainCellValueId);
				if (!str.isEmpty()) {
					reportExplainTextFillEntity.setExplainCellText(str.substring(0, str.length() - 1));
				}
				reportExplainTextFillEntity.setDzzId(summaryDzzId);
				reportExplainTextFillEntity.setExplainId(explainId);
				reportExplainTextFillEntityList.add(reportExplainTextFillEntity);

				ReportExplainFillEntity reportExplainFillEntity = new ReportExplainFillEntity();
				reportExplainFillEntity.setExplainCellDataId(CommonUtil.getUUID());
				reportExplainFillEntity.setAbsColNum(rptCellExplain.getAbsColNum() + "");
				reportExplainFillEntity.setAbsRowNum(rptCellExplain.getAbsRowNum() + "");
				reportExplainFillEntity.setExplainCellValue(explainCellValueId);
				reportExplainFillEntity.setDzzId(summaryDzzId);
				reportExplainFillEntity.setSumStatus("1");
				reportExplainFillEntity.setExplainId(explainId);
				reportExplainFillEntity.setIsSameRow(summaryDzzId);
				reportExplainFillEntityList.add(reportExplainFillEntity);
			}
		}
		rptExplainCellFillService.insertExplainCellValueList(annualId, reportExplainFillEntityList);
		rptExplainCellFillService.insertExplainCellTextValueList(annualId, reportExplainTextFillEntityList);
	}

	public void explainCellValueSummary(final String dzzId, final String annualId) {
		//先查出来当前年度下的所有的报表说明来
		List<RptTemplateExplain> rptTemplateExplainList = reportTemplateExplainService
				.getAllRptTemplateExplainByAnnualId(annualId);
		final List<String> rptTemplateExplainListTMId = new ArrayList<String>();
		final List<String> rptTemplateExplainListSZId = new ArrayList<String>();
		final List<String> rptTemplateExplainListWBId = new ArrayList<String>();

		for (RptTemplateExplain rptTemplateExplain : rptTemplateExplainList) {
			if ("1".equals(rptTemplateExplain.getExplainType())) {
				rptTemplateExplainListTMId.add(rptTemplateExplain.getId());
			} else if ("2".equals(rptTemplateExplain.getExplainType())) {
				rptTemplateExplainListSZId.add(rptTemplateExplain.getId());
			} else if ("3".equals(rptTemplateExplain.getExplainType())) {
				rptTemplateExplainListWBId.add(rptTemplateExplain.getId());
			} else {
			}
		}

		List<DnDzz> dNDzzList = dao.query(DnDzz.class, Cnd.where("SJDZZ_ID", "=", dzzId));
		final List<String> dnDzzIdList = new ArrayList<String>();
		for (DnDzz dnDzz : dNDzzList) {
			dnDzzIdList.add(dnDzz.getDzzid());
		}
		dnDzzIdList.add(dzzId);

		new Thread(new Runnable() {
			@Override
			public void run() {
				rptCellExplainSummaryTiaoMu(dnDzzIdList, rptTemplateExplainListTMId, annualId, dzzId);
				rptCellExplainSummaryShuZi(dnDzzIdList, rptTemplateExplainListSZId, annualId, dzzId);
				rptCellExplainSummaryWenBen(dnDzzIdList, rptTemplateExplainListWBId, annualId, dzzId);
			}
		}).start();
	}

	//删除已经汇总过的数据，方便重新汇总
	public void deleteByDzzIdAnIsSummary(String dzzId, String annualId, String explainId) {
		String tableName = ReportConstant.EXPLAIN_TABLE_PREFIX + annualId;
		String strSql = ReportConstant.EXPLAIN_DELETE_SQL.replace("$table_name", tableName);
		Sql sql = Sqls.create(strSql);
		sql.params().set("DZZ_ID", dzzId).set("EXPLAIN_ID", explainId);
		dao.execute(sql);
	}

	public Map<String, String> getRowMap(String annualId, String dzzId, String explainId, Integer num) {
		Map<String, String> mapRow = new HashMap<String, String>();
		String tableName = ReportConstant.EXPLAIN_TABLE_PREFIX + annualId;
		String strMinSql = "select min(abs_row_num) from " + tableName
				+ " where explain_id = @explainId and dzz_id = @dzzId and sum_status is null";
		String strMaxSql = "select max(abs_row_num) from " + tableName
				+ " where explain_id = @explainId and dzz_id = @dzzId and sum_status is null";
		Sql sqlMin = Sqls.create(strMinSql);
		Sql sqlMax = Sqls.create(strMaxSql);
		sqlMin.params().set("explainId", explainId).set("dzzId", dzzId);
		sqlMax.params().set("explainId", explainId).set("dzzId", dzzId);
		sqlMin.setCallback(Sqls.callback.integer());
		sqlMax.setCallback(Sqls.callback.integer());
		dao.execute(sqlMin);
		dao.execute(sqlMax);
		Integer minRow = sqlMin.getObject(Integer.class);
		Integer maxRow = sqlMax.getObject(Integer.class);
		//Integer differenceRow = maxRow - minRow;
		Integer rowNew = minRow + num;
		for (int a = minRow; a <= maxRow; a++) {
			mapRow.put(a + "", rowNew + "");
			rowNew++;
		}
		return mapRow;
	}

	public int getCountByDzzIdAndExplainId(String annualId, String dzzId, String explainId) {
		String tableName = ReportConstant.EXPLAIN_TABLE_PREFIX + annualId;
		String sqlStr = "select count(*) from (SELECT abs_row_num FROM " + tableName
				+ " where EXPLAIN_ID = @explainId and DZZ_ID = @dzzId and SUM_STATUS is null GROUP BY abs_row_num) t";
		Sql sql = Sqls.create(sqlStr);
		sql.params().set("dzzId", dzzId).set("explainId", explainId);
		sql.setCallback(Sqls.callback.integer());
		dao.execute(sql);
		return sql.getObject(Integer.class);
	}
}
